package com.yc.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.ssm.bean.ProductInfo;
import com.yc.ssm.biz.IProductInfoBiz;
import com.yc.ssm.util.ConstantInfo;
import com.yc.ssm.util.WaterMarkUtil;
import com.yc.ssm.vo.ResultVO;


@RestController
@RequestMapping("/product")
public class GoodsInfoController {
	@Autowired
	private IProductInfoBiz productInfoBiz;
	/**
	 * 处理富文本编辑器中图片上传
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	@PostMapping("/upload")
	public Map<String,Object> upload(MultipartFile upload) throws Exception{
		String savepath = ""; //保存图片的相对路径，这个路径是要存数据库的
		File dest = null; //最终要保存的文件
		Map<String,Object> result = new HashMap<String,Object>();

		if(upload != null && upload.getSize() > 0) { //说明确实有文件上传
			savepath = ConstantInfo.uploadpath + "/" + System.currentTimeMillis() + "_" + upload.getOriginalFilename();
			dest = new File(ConstantInfo.basepath, savepath);
			upload.transferTo(dest);
			
			WaterMarkUtil.textWaterMark(dest);
			
			//注意富文本编辑器ckeditor图片上传要求的返回值是一个map对象，里面包含fileName:文件名称, url：文件在服务器中的地址uploaded:回调函数
			result.put("fileName",upload.getOriginalFilename());
			result.put("url", "../../../" + savepath);
			result.put("uploaded", "1");
		}
		return result;
	}
	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@PostMapping("/add")
	public ResultVO add(ProductInfo pf, MultipartFile[] product_pics) {
		String savepath = ""; //保存图片的相对路径，这个路径是要存数据库的
		File dest = null; //最终要保存的文件
		
		if(product_pics != null && product_pics.length > 0 && product_pics[0].getSize() > 0) {
			String picsStr = "";
			try {
				for(MultipartFile pic : product_pics) {
					savepath = ConstantInfo.uploadpath + "/" + System.currentTimeMillis() + "_" + pic.getOriginalFilename();
					dest = new File(ConstantInfo.basepath, savepath);
					pic.transferTo(dest);//传输到指定的文件
					if("".equals(picsStr)) {
						picsStr = savepath;
					}else {
						picsStr += ";" + savepath;
					}
				}
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
		}
		
		int result = productInfoBiz.add(pf);
		//响应
		if(result > 0) {
			return new ResultVO(200,"成功");
		}
		return new ResultVO(500,"失败");

	}
	/**
	 * 第一次查询
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws Exception
	 */
	@PostMapping("/findFirst")
	protected ResultVO findFirst(@RequestParam Map<String,Object> map) throws IOException {
		Map<String, Object> result = productInfoBiz.findByFirst(map);
		if(result == null || result.isEmpty()) {
			return new ResultVO(600,"暂无数据");
		}
		return new ResultVO(200,result);
	}
	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@PostMapping("/finds")
	protected ResultVO finds(Map<String,Object> map) throws IOException {
		List<ProductInfo> list = productInfoBiz.finds(map);
		if(list == null || list.isEmpty()) {
			return new ResultVO(600,"暂无数据");
		}
		return new ResultVO(200,list);
	}
}
